





<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <title>表单预览</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/static/devtools30/js/formui/dlform.css">
    <link href="/static/devtools30/layui/css/layui.css" rel="stylesheet" />

    <link rel="stylesheet" type="text/css" href="/static/devtools30/js/formui/formdesign.css">
    <script type="text/javascript" src="/static/devtools30/js/formui/formdesign.js"></script>
    <script type="text/javascript" src="/static/devtools30/layui/layui.all.js"></script>
    <script type="text/javascript" src="/static/devtools30/js/formui/Sortable.min.js"></script>
    <script src="/static/devtools30/js/jquery-1.11.2.min.js" type="text/javascript"></script>
    <script src="/static/devtools30/js/formui/formControl.js"></script>
    <script src="/static/devtools30/js/devtoolCommon.js" type="text/javascript"></script>
    <title></title>
    <style>
        .dlong_hidden {
            background: #fef0f0 !important;
            display: none;
        }
    </style>
</head>

<body>


<div id="previewDiv" style="height: 100%;">
    <div style="padding: 20px 40px 0 0;" id="previewMain" lay-filter="devtoolform">

    </div>
    <div id="btnDiv" style="display: flex;justify-content: center;margin-top: 20px;">
        <button id="btnConfirm"  onclick="testSaveData()" type="button" style="background-color:#1890ff;border-radius: 4px;height: 30px;line-height: 30px;padding: 0 10px;" class="layui-btn">确定</button>
        <button id="btnClose"  type="button" style="border-radius: 4px;height: 30px;line-height: 30px;padding: 0 10px;" onclick="closeWin()" class="layui-btn layui-btn-primary">关闭</button>
    </div>

</div>
</body>


<script>
    var height=document.body.clientHeight;
    var width=document.body.clientWidth;
    var callback=dlCommon.getUrlParam("callback");
    var configjson = JSON.parse(unescape('[(${loadFormJson})]'));
    var previewList=[];
    $(function () {
        if(configjson.config.formloadurl&&configjson.config.formloadurl.trim().length>0){
            var formloadurl = configjson.config.formloadurl;
            if(formloadurl.indexOf("?")==-1){
                formloadurl=formloadurl+"?1=1";
            }
            formloadurl= formloadurl + "[(${bitoken})]&tempid=[(${tempid})]";
            if('[(${dataid})]'!=''){
                formloadurl=formloadurl+"&dataid=[(${dataid})]"
            }
            dlCommon.getJSON(formloadurl,{},function (objdata) {
                if(objdata.success){
                    configjson.list.forEach(function(item){
                        if(item.type=="grid"){
                            item.columns.forEach(function(child){
                                child.list.forEach(
                                    function (oneFieldConfig) {
                                        if(objdata.data[oneFieldConfig.id]){
                                            oneFieldConfig.defaultValue=(objdata.data[oneFieldConfig.id]);
                                        }
                                    }
                                )
                            })
                        }else{
                            if(objdata.data[item.id]){
                                item.defaultValue=(objdata.data[item.id]);
                            }
                        }
                    })
                }
                preview();
            })
        }else{
            preview();
        }

    })

    function closeWin(){
        dlCommon.closeParentLayer();
    }

    function preview(dataInfo){
        if(dataInfo){
            configjson.list.forEach(function(item){
                if(item.type=="grid"){
                    item.columns.forEach(function(child){
                        child.list.forEach(
                            function (oneFieldConfig) {
                                if(dataInfo[oneFieldConfig.id]){
                                    oneFieldConfig.defaultValue=(dataInfo[oneFieldConfig.id]);
                                }
                            }
                        )
                    })
                }else{
                    if(dataInfo[item.id]){
                        item.defaultValue=(dataInfo[item.id]);
                    }
                }
            });
        }

        var allJson={
            list:configjson.list,
            config:{
                name:'[(${name})]',
                width:'[(${width})]',
                height:'[(${height})]',
                controlCss:configjson.config.controlCss,
                formCss:configjson.config.formCss,
                initjs:configjson.config.initjs,
                savejs:configjson.config.savejs
            }
        };

        if(!allJson.list||allJson.list.length<=0){
            return;
        }
        var formConfig=allJson.config;
        var fwidth=formConfig.width||(width-20);
        var fheight=(formConfig.height)||height-40;
        previewList=[];

        document.getElementById("previewMain").innerHTML="";
        if(formConfig.formCss){
            document.getElementById("previewMain").style.cssText=formConfig.formCss;
        }
        var fristNode= document.getElementById("previewMain");
        allJson.list.forEach(function(item){
            var el = document.createElement("div")
            el.style.cssText=formConfig.controlCss||"padding-bottom:15px;";
            if(item.hidden){
                el.style.cssText=el.style.cssText+';display: none;';
            }
            fristNode.appendChild(el);
            var input1 = formDesign.render(el, item);
            previewList.push(input1);
        })

        $("#previewMain .dl_widget").removeClass("dl_widget")
        $("#previewMain .dlong_col_list").removeClass("dlong_col_list");
        if(configjson.config.initjs){
            eval(configjson.config.initjs);
        }
    }

    function formvalueList(checkRequired) {
        checkRequired=checkRequired==undefined?true:checkRequired;
        var strlist=[];
        try {
            previewList.forEach(function (item) {
                if (item.getOption().type == "grid") {
                    item.childArray.forEach(function (child) {
                        var option = child.getOption();
                        if (checkRequired&&child.getValue() == "" && option.required) {
                            throw new Error(option.requiredMessage || (option.label + "必填"))
                        }

                        strlist.push(JSON.stringify({id: option.id, value: child.getValue()}));
                    })
                } else {
                    var option = item.getOption();
                    if (checkRequired&&item.getValue() == "" && option.required) {

                        throw new Error(option.requiredMessage || (option.label + "必填"))
                    }
                    strlist.push(JSON.stringify({id: option.id, value: item.getValue()}));
                }

            })
        } catch (e) {
           dlCommon.layerError(e.message);
           return ;
        }
        return strlist;
    }

    //预览 保存测试数据
    function testSaveData(){
        if(configjson.config.savejs){
          var result=  eval(configjson.config.savejs);
          if(result==false){
              return;
          }
        }

        console.log(layui.form.val('devtoolform'));
        var saveUrl = "/DevtFormDesignEntityController/toPreviewSave";
        if(configjson.config.formsaveurl&&configjson.config.formsaveurl.trim().length>0){
            saveUrl = configjson.config.formsaveurl;
        }
        if(saveUrl.indexOf("?")==-1){
            saveUrl=saveUrl+"?1=1";
        }
        saveUrl= saveUrl + "[(${bitoken})]&tempid=[(${tempid})]";
        if('[(${dataid})]'!=''){
            saveUrl=saveUrl+"&dataid=[(${dataid})]"
        }
        var formvalueList1 = formvalueList();
        if(!formvalueList1){
            return;
        }
        dlCommon.post(saveUrl,{strlist:JSON.stringify(formvalueList1)},function(res){
            if(res.success){
                if(callback){
                    eval("parent."+callback+"&&parent."+callback+"()")
                }
                dlCommon.layerSuccess("操作成功",function () {
                    closeWin();
                });


            }else{
                dlCommon.layerError(res.msg);
            }
        },true)


    }

</script>

</html>